//
//  ViewController.m
//  CoreDataTest
//
//  Created by dllo on 15/11/20.
//  Copyright © 2015年 赵连鹏. All rights reserved.
//

#import "ViewController.h"
#import "CoreDataManager.h"
#import "Student.h"

@interface ViewController ()

@property(nonatomic, strong)CoreDataManager *manager;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    // 创建数据管理器类
    self.manager = [CoreDataManager defaults];
    
    NSLog(@"url =========== %@", [self.manager applicationDocumentsDirectory]);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)add:(id)sender {
    
    // 创建实体描述类
    // 参数1: 描述的实体
    // 参数2: 数据管理器
    NSEntityDescription *description = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.manager.managedObjectContext];
    
    // 创建实体
    // 参数1: 实体描述
    // 参数2: 数据管理器
    Student *stu = [[Student alloc] initWithEntity:description insertIntoManagedObjectContext:self.manager.managedObjectContext];
    stu.name = @"媛媛";
    stu.age = @21;
    stu.sex = @"女";
    stu.hobby = @"看电视📺";
    stu.number = @3;
    
    [self.manager saveContext];
    
    NSLog(@"添加");
    
}

// 查找
- (IBAction)select:(id)sender {
    
    // 获取查询数据的请求 相当于数据库的查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
    
    // 添加约束  谓词
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"name", @"%@"], @"媛媛"];
    
    request.predicate = predicate;
    
    // 通过某个key给数组排序
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    
    [request setSortDescriptors:[NSArray arrayWithObjects:sort, nil]];
    
    NSError *error;
    
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    NSLog(@"%ld", array.count);
    
    NSLog(@"查询");
    
}

// 修改
- (IBAction)update:(id)sender {
    
    // 获取查询数据的请求 相当于数据库的查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
    
    // 添加约束  谓词
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"name", @"%@"], @"媛媛"];
    
    request.predicate = predicate;
    
    // 通过某个key给数组排序
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    
    [request setSortDescriptors:[NSArray arrayWithObjects:sort, nil]];
    
    NSError *error;
    
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    Student *stu = [array lastObject];
    stu.age = @18;
    
    [self.manager saveContext];
    
    NSLog(@"修改");
    
}

// 删除
- (IBAction)delect:(id)sender {
    
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"age", @"%@"], @18];
    
    request.predicate = predicate;
    
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES];
    
    [request setSortDescriptors:[NSArray arrayWithObjects:sort, nil]];
    
    NSError *error;
    
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    
    for (Student *stu in array) {
        [self.manager.managedObjectContext deleteObject:stu];
    }
    
    [self.manager saveContext];
    
    NSLog(@"删除");
    
}



@end
